package com.example.librarymanagementproject2.web.controller;

import com.example.librarymanagementproject2.Common.config.ResultVO;
import com.example.librarymanagementproject2.model.entity.BorrowRecord;
import com.example.librarymanagementproject2.service.BorrowRecordService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName BorrowRecordController
 * @Description TODO
 * @Author Kx
 * @Date 2025/10/29 8:50
 * @Version 1.0
 */
@RestController
@RequestMapping("/borrowRecord")
public class BorrowRecordController {
    @Autowired
    private BorrowRecordService borrowRecordService;

    /**
     * @param borrowRecord : 借阅图书信息
     * @param request      : 请求信息
     * @author: kx
     * @date: 2025/10/28 9:56
     * @description: 借阅图书
     */
    @PostMapping("/book")
    public ResultVO<String> checkoutBook(@RequestBody BorrowRecord borrowRecord, HttpServletRequest request) {
        // 设置用户ID
        borrowRecord.setUserId((Long) request.getAttribute("userId"));
        borrowRecordService.checkoutBook(borrowRecord);
        return ResultVO.success("借阅成功");
    }

   /** 
    * @author: kx
    * @date: 2025/10/29 11:13
    * @param verifyCode : 验证码
    * @return com.example.librarymanagementproject2.Common.config.ResultVO<java.lang.String>
    * @description: 验证码校验并修改借阅状态
   */
    @PostMapping("/verifyCodeAndUpdateStatus")
    public ResultVO<String> verifyCodeAndUpdateStatus(@RequestParam("verifyCode") String verifyCode, HttpServletRequest request) {
        // 获取用户Id
        Long userId = (Long) request.getAttribute("userId");
        borrowRecordService.verifyCodeAndUpdateStatus(verifyCode, userId);
        return ResultVO.success("验证码校验成功");
    }

    /**
     * @author: kx
     * @date: 2025/10/29 16:25
     * @param borrowRecordId : 借阅信息id
     * @return com.example.librarymanagementproject2.Common.config.ResultVO<java.lang.String>
     * @description: 处理图书归还
    */
    @PostMapping("/processBookReturn")
    public ResultVO<String> processBookReturn(@RequestParam("borrowRecordId") Long borrowRecordId) {
        borrowRecordService.processBookReturn(borrowRecordId);
        return ResultVO.success("图书归还成功");
    }
}
